package com.ant.product.controller;

import com.alibaba.csp.sentinel.EntryType;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.fastjson.JSONObject;
import com.ant.product.entity.Student;
import com.ant.product.service.StudentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RefreshScope//动态刷新nacos配置
@RequestMapping("/product")
public class StudentController {

    static final Logger LOGGER = LoggerFactory.getLogger(StudentController.class);

    @Autowired
    private StudentService studentService;

    @Value("${product.user.name}")
    private String username;

    /**
     *  1、自定义注解   过滤器：拦截除来自网关的请求之外的其它请求
     *     2、集成Redis
     *      3、集成Kafka
     *      4、集成ElasticSearch
     *       TODO       做登录，存session、token
     *       TODO       网关跨域问题处理，统一异常处理
     *     5、断路由 sentinel
     *     6、session共享
     *      7、链路追踪  zipkin || SkyWalking
     *      8、分库分表
     * @return
     */

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private KafkaTemplate kafkaTemplate;

    @GetMapping("/list")
//    @MyAnnotation
    @SentinelResource(value = "getAllStudentList", blockHandler = "blockHand", entryType = EntryType.OUT)
    public Map<String, Object> getList(@RequestBody JSONObject json, HttpServletRequest request){
        LOGGER.info("=======request param=======" + json);
//        redisTemplate.opsForValue().set(json.getString("key"), json.getString("value"));
        List<Student> list = studentService.getAllStudent(null);
        Map<String, Object> map = new HashMap<>();
        map.put("data", list);
        return map;
    }

    public Map<String, Object> blockHand(@RequestBody JSONObject json, HttpServletRequest request, BlockException e){
        Map<String, Object> map = new HashMap<>();
        map.put("message", "服务繁忙");
        return map;
    }

    @GetMapping("/test")
    public Map<String, Object> test(@RequestParam String param){
        //测试redis
//        String str = redisTemplate.opsForValue().get(param);
        Map<String, Object> map = new HashMap<>();
//        map.put("data", str);
        map.put("username", username);
        return map;
    }

    @PostMapping("/kafka/producer")
    public Map<String, Object> kafkaProducerTest(@RequestBody JSONObject json){
        //key用于保证消费的顺序性和一致性
        kafkaTemplate.send("test", json.getInteger("partition"),
                json.getString("key"), json.getString("data"));
        Map<String, Object> map = new HashMap<>();
        map.put("data", "success");
        return map;
    }
}
